<?php
namespace user;

use View;
use Input;
use User;
use Validator;
use Redirect;
use Auth;
use Hash;

class IndexController extends \BaseController {

	public function __construct(){
		$this->beforeFilter('csrf',array('on'=>'post'));
	}

	/**
	 * Display a listing of the resource.
	 * GET /user/index
	 *
	 * @return Response
	 */
	public function index()
	{
		return View::make('user.index.index');
	}

	/**
	 * 用户登录页
	 * @return [type] [description]
	 */
	public function login(){
		return View::make('user.index.login');
	}

	public function doLogin(){
		$rules = array(
			'password'=>'required|min:6',
			'email' => 'required|email'
		);


		$validator = Validator::make(Input::all(),$rules);
		if($validator->passes()){
			$input = array(
				'password'=>Input::get('password'),
			'email' => Input::get('email')
			);

			if(Auth::attempt($input)){
				return Redirect::to('user/index');
			}else{
				return Redirect::back()->withInput()->with('message',array('type'=>'danger','content'=>'登录失败，账号和密码不一致'));
			}
		}else{
			return Redirect::back()->withInput()->withErrors($validator);
		}
	}


	public function logout(){
		if(Auth::check()){
			Auth::logout();
		}

		Redirect::to('user/login');
	}


	public function register(){
		return View::make('user.index.register');
	}

	public function doRegister(){
		$rules = array(
			'email'=>'required|email|unique:users',
			'username'=>'required|min:4|max:20|unique:users',
			'password'=>'required|min:6|confirmed',
			'password_confirmation'=>'required|min:6'
		);

		$validator = Validator::make(Input::all(),$rules);
		if($validator->passes()){
			$user = User::create(Input::only('email','username','password'));
			$user->password = Hash::make(Input::get('password'));

			if($user->save()){
				//注册成功自动登录
				Auth::login($user);
				return Redirect::to('user/index');
			}else{
				return Redirect::to('user/register')->with('message',array('type'=>'danger','content'=>'注册失败，请重试'))->withInput();
			}
		}else{
			return Redirect::to('user/register')->withInput()->withErrors($validator);
		}
	}

	public function resetPassword(){
		return View::make('user.index.resetpassword')->with('title','重设密码');
	}

	public function doResetPassword(){
		$rules = array(
			'email'=>'required|email',
			'oldpassword'=>'required|min:6',
			'password'=>'required|min:6|confirmed',
			'password_confirmation'=>'required|min:6'
		);

		$validator = Validator::make(Input::all(),$rules);
		if($validator->passes()){
			$oldpassword = Hash::make(Input::get('oldpassword'));
			$user = User::find(Auth::id());
			if(!(Hash::check(Input::get('oldpassword'),$user->password))){
				return Redirect::to('user/resetpassword')->withInput()->with('message',array('type'=>'danger','content'=>'旧密码错误'));
			}

			$user->password = Hash::make(Input::get('password'));

			if($user->save()){
				return Redirect::to('user/resetpassword')->with('message',array('type'=>'success','content'=>'密码修改成功'));
			}else{
				return Redirect::to('user/resetpassword')->with('message',array('type'=>'danger','content'=>'密码修改失败，请重试'))->withInput();
			}
		}else{
			return Redirect::to('user/resetpassword')->withInput()->withErrors($validator);
		}
	}

}